package speedball.popup;

//import com.jfoenix.controls.JFXPopup;
import com.jfoenix.effects.JFXDepthManager;
import javafx.geometry.Insets;
import javafx.scene.control.Skin;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;

        /*
         * Licensed to the Apache Software Foundation (ASF) under one
         * or more contributor license agreements.  See the NOTICE file
         * distributed with this work for additional information
         * regarding copyright ownership.  The ASF licenses this file
         * to you under the Apache License, Version 2.0 (the
         * "License"); you may not use this file except in compliance
         * with the License.  You may obtain a copy of the License at
         *
         *   http://www.apache.org/licenses/LICENSE-2.0
         *
         * Unless required by applicable law or agreed to in writing,
         * software distributed under the License is distributed on an
         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
         * KIND, either express or implied.  See the License for the
         * specific language governing permissions and limitations
         * under the License.
         */
//        import com.jfoenix.controls.JFXPopup.PopupHPosition;
//        import com.jfoenix.controls.JFXPopup.PopupVPosition;

import speedball.popup.MyPopup.PopupHPosition;
import speedball.popup.MyPopup.PopupVPosition;


import javafx.scene.Node;
import javafx.scene.layout.*;
        import javafx.scene.paint.Color;

/**
 * <h1>Material Design Popup Skin</h1>
 * TODO: REWORK
 * @author Shadi Shaheen
 * @version 2.0
 * @since 2017-03-01
 */
public class MyPopupSkin implements Skin<MyPopup> {

    protected MyPopup control;
    protected StackPane container = new StackPane();
    protected Region popupContent;
    protected Node root;

//    protected Scale scale;

    public MyPopupSkin(MyPopup control) {
        this.control = control;
        // set scale y to 0.01 instead of 0 to allow layout of the content,
        // otherwise it will cause exception in traverse engine, when focusing the 1st node
//        scale = new Scale(1, 0.01, 0, 0);
        popupContent = control.getPopupContent();
        container.getStyleClass().add("jfx-popup-container");
        container.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
        container.getChildren().add(popupContent);
//        container.getTransforms().add(scale);
        container.setOpacity(1);
        root = JFXDepthManager.createMaterialNode(container, 4);
        root.maxWidth(container.getWidth());
        root.maxHeight(container.getHeight());
    }


    public void reset(PopupVPosition vAlign, PopupHPosition hAlign, double offsetX, double offsetY) {
        // postion the popup according to its animation
//        scale.setPivotX(hAlign == PopupHPosition.RIGHT ? container.getWidth() : 0);
//        scale.setPivotY(vAlign == PopupVPosition.BOTTOM ? container.getHeight() : 0);
        root.setTranslateX(hAlign == PopupHPosition.RIGHT ? -container.getWidth() + offsetX : offsetX);
        root.setTranslateY(vAlign == PopupVPosition.BOTTOM ? -container.getHeight() + offsetY : offsetY);
    }


    @Override
    public MyPopup getSkinnable() {
        return control;
    }

    @Override
    public Node getNode() {
        return root;
    }

    @Override
    public void dispose() {
        container = null;
        control = null;
        popupContent = null;
        root = null;
    }

    public void init() {
        container.setOpacity(1);
    }
}
